import QQMapWX from '@/libs/qqmap-wx-jssdk.min.js'

const qqmapsdk = new QQMapWX({
    key: '6QXBZ-56FK5-26IIH-QQM4H-NNTVV-DIBNX'
});

export const wxLocation = function (type, para = {}) {
    return new Promise((resolve, reject) => {
        uni[type]({
            ...para,
            success(res) {
                console.log('type：' + JSON.stringify(res));
                resolve(res)
            },
            fail() {
                // #ifdef MP-WEIXIN
                uni.getSetting({
                    success(res) {
                        var statu = res.authSetting
                        if (!statu['scope.userLocation']) {
                            uni.showModal({
                                title: '是否授权当前位置',
                                content: '需要获取您的地理位置，请确认授权，否则地图功能将无法使用',
                                success(tip) {
                                    if (tip.confirm) {
                                        uni.openSetting({
                                            success(data) {
                                                if (data.authSetting['scope.userLocation'] === true) {
                                                    uni.showToast({
                                                        title: '授权成功',
                                                        icon: 'success',
                                                        duration: 1000
                                                    })
                                                    //授权成功之后，再调用chooseLocation选择地方
                                                    uni[type]({
                                                        ...para,
                                                        success(res) {
                                                            resolve(res)
                                                        }
                                                    })
                                                } else {
                                                    reject()
                                                    uni.showToast({
                                                        title: '授权失败',
                                                        icon: 'none',
                                                        duration: 1000
                                                    })
                                                }
                                            }
                                        })
                                    }
                                }
                            })
                        }
                    },
                    fail(res) {
                        reject()
                        uni.showToast({
                            title: '调用授权窗口失败',
                            icon: 'none',
                            duration: 1000
                        })
                    }
                })
                // #endif
                //在h5里面
                // #ifdef H5
                uni.getLocation({
                    type: 'wgs84',
                    success: res => {
                        console.log(res)
                        resolve(res)
                    },
                })
                // #endif

            }
        })
    })
}

// 获取当前地理位置
export const getPosition = ({
                            latitude,
                            longitude
                        }) => {
    return new Promise((resolve, reject) => {
        let vm = this;
        qqmapsdk.reverseGeocoder({
            location: {
                latitude: latitude,
                longitude: longitude
            },
            success: function(res) {
                console.log('res',res);
                let province = res.result.address_component.province
                let city = res.result.address_component.city
                let district = res.result.address_component.district
                let street = ''
                if(res.result.address_component){
                    street = res.result.address_component.street
                }
                let cityName = '';
                let town = ''
                if(res.result.address_reference && res.result.address_reference.town){
                    town = res.result.address_reference.town.title
                }
                let address = res.result.address;
                let name = res.result.address_component.street_number;
                resolve({
                    address: address,
                    name: name,
                    province: province,
                    city: city,
                    cityName: cityName,
                    latitude: latitude,
                    longitude: longitude,
                    district: district,
                    street: street,
                    town: town
                })
            },
            fail: function(res) {
                console.log(res);
                reject()
            },
            complete: function(res) {
                console.log(res);
            }
        });
    })
}